package org.learn.intermediate;

public class Encapsulation {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("红红");
        person.setAge(150);
        person.setSalary(2488.4);


        System.out.println(person.info());

    }
}
class Person {
    public String name;//名字公开
    private int age;//私有化
    private double salary;//私有化
    //快捷键搞定
//    完善代码
    //如果有构造器则会跳过验证，解决办法是将set方法写入构造器中；防护机制生效；

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //加入判断
        this.name = name;
    }

    public int getAge() {

        return age;
    }

    public void setAge(int age) {
        //判断
        if(age > 1 && age < 120) {
            this.age = age;
        }else{
            System.out.println("年龄需要在1-120 之间，请不要乱输入");
        }
    }

    public double getSalary() {
        //加入判断条件，业务逻辑；
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String info(){
       return  "名字=" + name +"年龄=" + age +"工资=" + salary;
    }
}